//
//  H24Controller.m
//  网易新闻
//
//  Created by dllo on 15/10/10.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "H24Controller.h"
#import "DetailsController.h"
#import "XWTableViewCell_b.h"
#import "XWTableViewCell_a.h"
#import "XinwenModel.h"
#import "Settings.h"
@interface H24Controller ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)DetailsController *result;
@end
@implementation H24Controller

- (void)viewWillLayoutSubviews{
    [self.table.header beginRefreshing];
}

- (void)createTableView{
    [self setTableView];
    self.table.delegate = self;
    self.table.dataSource = self;
    
    [self.table registerClass:[XWTableViewCell_a class] forCellReuseIdentifier:@"XWTableViewCell_a"];
    [self.table registerClass:[XWTableViewCell_b class] forCellReuseIdentifier:@"XWTableViewCell_b"];
    self.table.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handelData];
        [self.table.header endRefreshing];
    }];
}

- (void)handelData{
    NSString *url = @"http://c.m.163.com/nc/article/list/T1429173762551/0-20.html";
    [AFTool GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        self.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"T1429173762551"]];
        [[Settings shareSettings]writeToDBWithDic:tempDic WithName:url];
        [tempDic release];
        [self.table reloadData];
    } fail:^(NSError *error) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:[[Settings shareSettings]dicSelectFromDB:url]];
        if (![tempDic isEqual:nil]) {
            self.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"T1429173762551"]];
            [tempDic release];
            [self.table reloadData];
        }
        NSLog(@"404(H24Controller)");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XinwenModel *mod = [XinwenModel modWithDic:self.modArr[indexPath.row]];
    if (indexPath.row == 0) {
        XWTableViewCell_b *cell = [[XWTableViewCell_b alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XWTableViewCell_b"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }else{
        XWTableViewCell_a *cell = [[XWTableViewCell_a alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XWTableViewCell_a"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.result = [[DetailsController alloc]init];
    self.result.docid = [self.modArr[indexPath.row]objectForKey:@"docid"];;
    [self.navigationController pushViewController:self.result animated:YES];
    [self.result release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (void)dealloc{
    self.table.dataSource = nil;
    self.table.delegate = nil;
    [super dealloc];
}


@end
